const mongoose = require('mongoose')
const uuidv4 = require('uuid/v4')
mongoose.Promise = global.Promise

const MenuSchema = new mongoose.Schema({
  order: {type: Number, default: 0},
  _id: {type: String, default: uuidv4()},
  pid: String,
  icon: String,
  label: {type: String, required: true, index: {unique: true}},
  uri: {type: String, default: ''},
  isShow: {type: Boolean, default: true}, // 是否显示
  isDevShow: {type: Boolean, default: true}, // 是否在开发模式下显示
  description: String,
  createIP: String,
  updateIP: String,
  meta: {
    createAt: { type: Date, default: Date.now() },
    updateAt: { type: Date, default: Date.now() }
  },
  key: {type: String, default: uuidv4()}
})

MenuSchema.pre('save', function (next) {
  if (!this.isNew) {
    this.registerAt = Date.now()
    this.meta.createAt = this.meta.updateAt = Date.now()
  }
  next()
})

MenuSchema.statics = {
  fetch (cb) {
    this.find({}).exec(cb)
  }
}

module.exports = MenuSchema
